package com.tanhua.server.service;

import com.tanhua.dubbo.api.mongo.OptionsApi;
import com.tanhua.dubbo.api.mongo.QuestionnaireApi;
import com.tanhua.dubbo.api.mongo.QuestionsApi;
import com.tanhua.dubbo.api.mongo.SoulApi;
import com.tanhua.model.mongo.Options;
import com.tanhua.model.mongo.QuestionBank;
import com.tanhua.model.mongo.Questions;
import com.tanhua.model.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author mdoya
 * @version 1.0
 * @description: 灵魂测试业务层
 */
@Slf4j
@Service
public class SoulService {

    @DubboReference
    private SoulApi soulApi;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @DubboReference
    private QuestionnaireApi questionnaireApi;

    @DubboReference
    private QuestionsApi questionsApi;

    @DubboReference
    private OptionsApi optionsApi;

    /**
     * 测灵魂--问卷列表
     * 实现步骤
     * 1.调用服务查询相关数据
     * 2.基于查询数据封装vo返回
     *
     * @return
     */
    public List<QuestionnaireVo> findSoul() {
        //1.调用服务查询问卷列表获取问卷列表相关数据
        List<QuestionBank> questionBankList = questionnaireApi.findSoul();

        List<QuestionnaireVo> questionnaireVoList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(questionBankList)) {

            for (QuestionBank questionBank : questionBankList) {
                QuestionnaireVo questionnaireVo = new QuestionnaireVo(); // 响应的vo
                //2.获取问卷的等级
                String level = questionBank.getLevel();

                //3.根据问卷难度查询对应的题目
                List<Questions> questionsList = questionsApi.findQuestion(level);

                //创建QuestionsVo封装对象
                List<QuestionsVo> questionsVoList = new ArrayList<>();

                if (!CollectionUtils.isEmpty(questionsList)) {
                    for (Questions questions : questionsList) {
                        String questionsId = questions.getId().toHexString();
                        //4.通过问题id去查询选项表，获取选项
                        List<Options> optionsList = optionsApi.findOption(questionsId);
                        //封装optionsVo
                        List<OptionsVo> optionsVoList=new ArrayList<>();
                        if (!CollectionUtils.isEmpty(optionsList)){
                            for (Options options : optionsList) {
                                //为options设置数据
                                OptionsVo optionsVo = new OptionsVo();
                                optionsVo.setId(options.getId().toHexString()); //设置id
                                optionsVo.setOption(options.getOption());  //设置Option
                                optionsVoList.add(optionsVo);
                            }

                        }
                        //封装QuestionsVo
                        QuestionsVo qs = new QuestionsVo();
                        qs.setQuestion(questions.getQuestion());
                        qs.setId(questions.getId().toHexString());
                        qs.setOptions(optionsVoList);

                        questionsVoList.add(qs);
                    }
                }

                //5.基于查询结果封装vo返回
                //5.1.将questionBank拷贝到questionnaireVo中
                BeanUtils.copyProperties(questionBank, questionnaireVo);
                questionnaireVo.setId(questionBank.getId().toHexString());//设置String id
                questionnaireVo.setQuestions(questionsVoList);
//                questionnaireVo.setReportId("1");
                questionnaireVoList.add(questionnaireVo);

            }
        }
        questionnaireVoList.stream().forEach(System.err::println);//在控制台打印结果数据
           return questionnaireVoList;
    }}
